package com.tencent.qcloud.timchat.utils;

import android.content.Context;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.arlen.photo.photopickup.util.FileSizeUtil;
import com.tencent.imsdk.TIMCallBack;
import com.tencent.imsdk.TIMFileElem;
import com.tencent.imsdk.TIMMessage;
import com.wbao.dianniu.R;
import com.wbao.dianniu.logical.DnApplication;
import com.wbao.dianniu.utils.FileUtil;
import com.wbao.dianniu.utils.Notification;


/**
 * 文件消息
 */
public class FileMessage extends Message {


    public FileMessage(TIMMessage message) {
        this.message = message;
    }

    public FileMessage(String filePath) {
        message = new TIMMessage();
        TIMFileElem elem = new TIMFileElem();
        elem.setPath(filePath);
        elem.setFileName(filePath.substring(filePath.lastIndexOf("/") + 1));
        message.addElement(elem);
    }


    /**
     * 显示消息
     *
     * @param viewHolder 界面样式
     * @param context    显示消息的上下文
     */
    @Override
    public void showMessage(ChatAdapter.ViewHolder viewHolder, Context context) {
        clearView(viewHolder);
        if (checkRevoke(viewHolder)) return;
        // 文件 恢复背景色
        if (message.isSelf()) {
            viewHolder.rightMessage.setBackgroundResource(R.drawable.right_file_chat_bg);
        } else {
            viewHolder.leftMessage.setBackgroundResource(R.drawable.left_chat_bg);
        }

        /*
        TIMFileElem e = (TIMFileElem) message.getElement(0);
        TextView tv = new TextView(DnApplication.getContext());
        tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
        tv.setTextColor(DnApplication.getContext().getResources().getColor(isSelf() ? R.color.white : R.color.black));
        String fileName = e.getFileName();
        tv.setId(1);
        tv.setText(fileName);
        RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(700, RelativeLayout.LayoutParams.MATCH_PARENT);
        tv.setLayoutParams(lp1);
        getBubbleView(viewHolder).addView(tv);

        View msg = getBubbleView(viewHolder).getChildAt(0);
        ImageView fileIcon = new ImageView(DnApplication.getContext());
        RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(200, 200);
        lp2.addRule(RelativeLayout.RIGHT_OF, msg.getId());
        lp2.rightMargin = 20;
        lp2.topMargin = 20;
        lp2.leftMargin = 10;
        fileIcon.setLayoutParams(lp2);
        if (fileName.endsWith(".doc") || fileName.endsWith(".docx")) {
            fileIcon.setImageResource(R.drawable.word_icon);
        } else if (fileName.endsWith(".xls") || fileName.endsWith(".xlsx")) {
            fileIcon.setImageResource(R.drawable.xls_icon);
        } else if (fileName.endsWith(".ppt") || fileName.endsWith(".pptx")) {
            fileIcon.setImageResource(R.drawable.ppt_icon);
        } else if (fileName.endsWith(".pdf") || fileName.endsWith(".PDF")) {
            fileIcon.setImageResource(R.drawable.pdf_icon);
        } else {
            fileIcon.setImageResource(R.drawable.text_icon);
        }
        getBubbleView(viewHolder).addView(fileIcon);

        TextView fileSize = new TextView(DnApplication.getContext());
        fileSize.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
        fileSize.setTextColor(DnApplication.getContext().getResources().getColor(R.color.contact_text));
        RelativeLayout.LayoutParams lp3 = new RelativeLayout.LayoutParams(700, RelativeLayout.LayoutParams.MATCH_PARENT);
        lp3.addRule(RelativeLayout.BELOW, msg.getId());
        fileSize.setText(FileSizeUtil.FormetFileSize(e.getFileSize()));
        lp3.topMargin = 20;
        lp3.leftMargin = 10;
        fileSize.setLayoutParams(lp3);
        getBubbleView(viewHolder).addView(fileSize);
        showStatus(viewHolder);
        */
        View view = LayoutInflater.from(context).inflate(R.layout.file_message_include, null);
        ImageView fileIcon = view.findViewById(R.id.file_icon_iv);
        TextView nameTV = view.findViewById(R.id.file_name_tv);
        TextView sizeTV = view.findViewById(R.id.file_size_tv);

        TIMFileElem e = (TIMFileElem) message.getElement(0);
        String fileName = e.getFileName();
        nameTV.setText(fileName);
        sizeTV.setText(FileSizeUtil.FormetFileSize(e.getFileSize()));
        if (fileName.endsWith(".doc") || fileName.endsWith(".docx")) {
            fileIcon.setImageResource(R.drawable.word_icon);
        } else if (fileName.endsWith(".xls") || fileName.endsWith(".xlsx")) {
            fileIcon.setImageResource(R.drawable.xls_icon);
        } else if (fileName.endsWith(".ppt") || fileName.endsWith(".pptx")) {
            fileIcon.setImageResource(R.drawable.ppt_icon);
        } else if (fileName.endsWith(".pdf") || fileName.endsWith(".PDF")) {
            fileIcon.setImageResource(R.drawable.pdf_icon);
        } else {
            fileIcon.setImageResource(R.drawable.text_icon);
        }
        getBubbleView(viewHolder).addView(view);
        showStatus(viewHolder);

    }

    /**
     * 获取消息摘要
     */
    @Override
    public String getSummary() {
        String str = getRevokeSummary();
        if (str != null) return str;
        return DnApplication.getContext().getString(R.string.summary_file);
    }

    /**
     * 保存消息或消息文件
     */
    @Override
    public void save() {
        if (message == null) return;
        final TIMFileElem e = (TIMFileElem) message.getElement(0);
        String[] str = e.getFileName().split("/");
        String filename = str[str.length - 1];
        final String path = FileUtil.getStoreFile();
        if (FileUtil.isFileExist(filename, path)) {
            Notification.toast(DnApplication.getContext(), DnApplication.getContext().getString(R.string.save_exist));
            return;
        }
        e.getToFile(FileUtil.getStoreFilePath(filename), new TIMCallBack() {
            @Override
            public void onError(int i, String s) {
                Log.e(TAG, "getFile failed. code: " + i + " errmsg: " + s);
            }

            @Override
            public void onSuccess() {
                Notification.toast(DnApplication.getContext(), "文件已保存到：" + path);
            }
        });

    }
}
